package com.yl.navigation.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexboxLayout
import com.yl.common.exp.Constants
import com.yl.navigation.R
import com.yl.navigation.bean.TreeBean
import java.util.ArrayList

class TreeAdapter(private val listener: TreeItemClickListener) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>(), View.OnClickListener {

    private var diff: AsyncListDiffer<TreeBean>

    init {
        diff = AsyncListDiffer(this, MyCallback())
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_sys_rv, parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val data = diff.currentList[position]

        Log.e("----vbbbbb-------", "-------------------------------")
        (holder as MyViewHolder).name.text = data.name
        holder.flex.removeAllViews()

        Log.e("----ccc-------", data.toString())

        for (i in data.articles.indices) {
            val layout = LayoutInflater.from(holder.itemView.context)
                .inflate(R.layout.item_flex, null, false)
            val t = layout.findViewById<TextView>(R.id.tx_flex)
            t.text = data.articles[i].title

            layout.tag = TreeItemEvent(position, i)
            layout.setOnClickListener(this)

            holder.flex.addView(layout)
        }
    }

    override fun getItemCount(): Int {
        return diff.currentList.size
        Log.e("----mmmm-------",  diff.currentList.size.toString())
    }

    fun setData(list: List<TreeBean>?) {
        diff.submitList(if (list != null) ArrayList(list) else null)
        Log.e("----eeee-------", list!!.size.toString())
    }

    class MyViewHolder(item: View) : RecyclerView.ViewHolder(item) {
        var name: TextView = item.findViewById(R.id.tx_sys_rv)
        var flex: FlexboxLayout = item.findViewById(R.id.flex_sys)
    }

    class MyCallback : DiffUtil.ItemCallback<TreeBean>() {
        override fun areItemsTheSame(
            oldItem: TreeBean,
            newItem: TreeBean
        ): Boolean {
            return oldItem.cid == newItem.cid
        }

        override fun areContentsTheSame(
            oldItem: TreeBean,
            newItem: TreeBean
        ): Boolean {
            return oldItem.name == newItem.name
        }
    }

    private var lastClickTime: Long = 0
    override fun onClick(v: View?) {
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastClickTime > Constants.MIN_CLICK_DELAY_TIME && v != null) {
            lastClickTime = currentTime
            listener.onClick(v.tag as TreeItemEvent)
        }
    }
}